﻿#include"head.h"

/*初始化商品选项链表*/
void CreatSingleLinkInit(ptask good)
{
	LinkList* p1 = &(good->choosegood);
	(*p1) = (LinkList)malloc(sizeof(struct choose_goodlink));
	if(*p1 == NULL)
	{
		perror("CreateSingleLink failed");
		exit(0);
	}
	bzero((*p1)->commoditycode,sizeof((*p1)->commoditycode));
	(*p1)->next = NULL;
}

//初始化双向链表，相当于头节点
void DLinkListInit(ptask good)
{	
	ptask p1 = good;
	DLinkList *head = &(p1->goodlist);
	(*head) = (struct good_struct*)malloc(sizeof(struct good_struct));
	if(*head == NULL)
	{
		perror("DLinkListInit failed");
		exit(0);
	}
	//printf("(*head) ->attr.spec:%f\n",(*head)->attr.spec);
	(*head)->prior == NULL;
	bzero(&((*head)->attr),sizeof(struct good_attribute)); 
	(*head)->next == NULL;
	//printf("(*head) ->attr.spec:%f\n",(*head)->attr.spec);
}

DLinkList AddNewNode(ptask p,char *commoditycode)
{
	int index = 0;
	DLinkList new_node = (DLinkList)malloc(sizeof(struct good_struct));
	if(new_node == NULL)
	{
		perror("new_node malloc failed");
		exit(-1);
	}
	/*找到此商品所在位置,如果在此仓库中没找到此商品，则返回NULL*/
	for(index;index <= p->allgoodlist.index;index++)
	{
		if(strcmp(commoditycode,p->allgoodlist.attr[index].commoditycode)==0)
		{
			new_node->prior = NULL;
			memcpy(&(new_node->attr),&(p->allgoodlist.attr[index]),sizeof(struct good_attribute));
			new_node->next = NULL;
			/*计算价格*/
			p->totalprice += new_node->attr.price;
			//printf("%s,%f,%d\n",commodity->allgoodlist.attr[3].commodityname,commodity->allgoodlist.attr[6].price,commodity->allgoodlist.index);

			return new_node;
		}	
	}
	return NULL;
}

/*将登记的商品加入到双向链表*/
bool GoodEnterUp(ptask good)
{
	ptask tempgood = good; 
	DLinkList temp = NULL;
	LinkList p1 = good->choosegood; //编码单链表
	LinkList q1= p1; //当一个商品编码使用过后，从链表中移除
	DLinkList p2 = good->goodlist;
	while(p1->next != NULL)
	{
		p1 = p1->next;
		temp = AddNewNode(tempgood,p1->commoditycode);
		if(temp != NULL)
		{
			while(p2->next != NULL)
				p2 = p2->next;
			p2->next = temp;
			temp->prior = p2;
			p2 = good->goodlist;   //p2重新指向头节点
		}else{
			printf("此仓库中没有您要找的商品:%s\n",p1->commoditycode);
		}
		/*将编码链表中已经操作过的节点的空间释放*/
		free(q1);//释放前一个节点
		q1 = p1;
	}
	free(q1);

	return true;
}

LinkList AddChooseNode(char* recomdtycode)
{
	LinkList p1 = (LinkList)malloc(sizeof(struct choose_goodlink));
	if(p1 == NULL)
	{
		perror("AddChooseNode malloc failed");
		exit(-1);
	}
	strcpy(p1->commoditycode,recomdtycode);
	p1->next = NULL;
	return p1;
}

void CreatSingleLink(ptask good,char *recomdtycode)
{
	char p2[COMMODITYCODESIZE];
	bzero(p2,COMMODITYCODESIZE);
	strcpy(p2,recomdtycode);
	
	LinkList temp = AddChooseNode(p2);
	LinkList p1 = good->choosegood;
	while(p1->next != NULL)
		p1 = p1->next;
	p1->next = temp;
	

}

void CancelPurchaseDataNode(ptask regood,char* recomdtycode)
{
	ptask good = regood;
	DLinkList p1 = regood->goodlist;
	DLinkList temp1 = p1;
	
	char p2[COMMODITYCODESIZE];
	bzero(p2,COMMODITYCODESIZE);
	strcpy(p2,recomdtycode);
	while(p1->next != NULL)
	{
		
		p1 = p1->next;
		if(strcmp(p1->attr.commoditycode,p2)==0)
		{
			 /*计算价钱*/
			 good->totalprice -= p1->attr.price;
			/*移除节点*/
			 temp1 = p1->prior;
			 p1->prior->next = p1->next;
			 if(p1->next != NULL)
				 p1->next->prior = p1->prior;
		}
	}
	
}

void GoodCancel(ptask good)
{
	ptask tempgood =good;
	LinkList  p1 = good->choosegood;
	LinkList  q1 = p1;
	while(p1->next != NULL)
	{
		p1 = p1->next;
		CancelPurchaseDataNode(tempgood,p1->commoditycode);
		/*将编码链表中已经操作过的节点的空间释放*/
		//if(q1->commoditycode)	
		free(q1);//释放前一个节点
		q1 = p1;
	}
	free(q1);
	
}

void CountPrine(ptask good)
{
	float totalprice = 0.0;
	totalprice = good->totalprice;
	puts("😊 ====💴 ====😊 ");
	printf("[You will pay]: %0.2f$ ✔\n",totalprice);
	puts("😊 ====💴 ====😊");
}


void FreeStoreSpace(ptask good)
{
		DLinkList p1 = good->goodlist;
		DLinkList p2=p1;
		while(p1->next != NULL)
		{
			p2 = p1;
			p1 = p1->next;
			free(p2);
		}
		free(p1);	
}
